package com.efast.cafe.portal.service.common.impl;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.portal.dao.base.PortalBaseLovDao;
import com.efast.cafe.portal.entity.base.PortalBaseLov;
import com.efast.cafe.portal.entity.base.PortalBaseParameter;
import com.efast.cafe.portal.service.common.ILovService;
import com.efast.cafe.util.StringUtil;

/******************************************************************************
 * @File name   :      LovServiceImpl.java
 *
 * @Author      :      xl
 *
 * @Date        :      2016-8-16
 *
 * @Copyright Notice: 
 * Copyright (c) 2016 Efast Serv. All  Rights Reserved.
 * This software is published under the terms of the Efast Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2016-8-16 下午3:46:15        xl     1.0            Initial Version
 *****************************************************************************/
@Service
@Transactional
@RestController
public class LovServiceImpl implements ILovService{

	private PortalBaseLovDao portalBaseLovDao ;
	
	@Qualifier("jdbcDao")
	@Autowired
	private JdbcDao jdbcDao;
	
	
	
	

	@Autowired
	public void setPortalBaseLovDao(PortalBaseLovDao portalBaseLovDao) {
		this.portalBaseLovDao=portalBaseLovDao;
	}

	/**
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2016-8-16
	 * @see org.eae.eldf.portal.remote.common.LovService#getParameter(java.lang.String, java.lang.String)
	**/
	@Override
	public List<PortalBaseLov> getNextNodeLov(PortalBaseParameter parameter) {
		Assert.notNull(parameter,"参数parameter对象不能为空");
		Assert.notNull(parameter.getParam_key(),"参数param_key不能为空");
		Criteria criteria=Criteria.create(PortalBaseLov.class).where(PortalBaseLov.paramKey, new Object[]{parameter.getParam_key()}).and(PortalBaseLov.parentId, new Object[]{0});
		if(StringUtil.isNotBlank(parameter.getEnterprise_id())){
			criteria.and(PortalBaseLov.enterpriseId, new Object[]{parameter.getEnterprise_id()});
		}
		List<PortalBaseLov> list=jdbcDao.queryList(criteria);
		boolean islast=true;
		List<PortalBaseLov> node=list;
		while(islast){
			if(node.size()>0){
				node=portalBaseLovDao.queryListNextNode(node);
				list.addAll(node);
			}else{
				islast=false;
			}
		}
		return list;
		
	}

	/**
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2016-8-16
	 * @see org.eae.eldf.portal.remote.common.LovService#getAllByOptionkey(java.lang.String)
	**/
	@Override
	public List<PortalBaseLov> getAllById(String id) {
		Criteria criteria=Criteria.create(PortalBaseLov.class).where(PortalBaseLov.Id, new Object[]{id});
		List<PortalBaseLov> list=jdbcDao.queryList(criteria);
		if(list.size()>0){
			Criteria criteriaList=Criteria.create(PortalBaseLov.class).where(PortalBaseLov.parentId, new Object[]{list.get(0).getPortal_base_lov_id()});
			list=jdbcDao.queryList(criteriaList);
		}
		
		return list;
	}

	
	    /* (非 Javadoc)
	    * 
	    * 
	    * @param id
	    * @return
	    * @see com.efast.cafe.portal.remote.common.LovService#api_getNextNodeLov(java.lang.String)
	    */
	    
	@Override
	public List<PortalBaseLov> api_getNextNodeLov(@RequestBody PortalBaseParameter parameter) {
		// TODO Auto-generated method stub
		return getNextNodeLov(parameter);
	}


}
